package com.IO流.Reader字符输入流;

import org.junit.jupiter.api.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Exercise01 {
    public static void main(String[] args) {
        /**
         * 从D:\\JAVA_test\\story.txt  文件中按字符读取出来
         */
    }

    String path = "D:\\JAVA_test\\story.txt";
    //按一个字符
    @Test
    public void test01() {
        int readLen = 0;
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(path);
            while ((readLen = fileReader.read()) != -1) {
                System.out.print((char) readLen);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    //按字符数组读取
    @Test
    public void test02() {
        int readLen = 0;
        FileReader fileReader = null;
        char[] chars = new char[64];
        try {
            fileReader = new FileReader(path);
            while ((readLen = fileReader.read(chars)) != -1) {
                System.out.print(new String(chars,0,readLen));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

}
